﻿using System;
using System.Collections.Generic;
using System.Linq;
using GTAuto.DAL.Entities;
using GTAuto.DAL.Enums;
using GTAuto.DAL.Extensions;

namespace GTAuto.DAL.DAL
{
    public class DALModelModificationProperty : DALBase
    {
        public static DALModelModificationProperty Singleton = new DALModelModificationProperty();

        public GTModelModificationProperty Add(GTModelModificationProperty gtModelModificationProperty)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var modProp = new ModelModificationProperty();
                modProp.ModelId = gtModelModificationProperty.ModelId;
                modProp.PropertyId = gtModelModificationProperty.PropertyId;
                db.ModelModificationProperties.InsertOnSubmit(modProp);
                db.SubmitChanges();

                return gtModelModificationProperty;
            }
        }


        public GTModelModificationProperty Get(int modelId, int propertyId)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var brand = db.ModelModificationProperties.SingleOrDefault(x => x.ModelId == modelId && x.PropertyId == propertyId);

                if (brand == null)
                    return null;

                var gtBrand = new GTModelModificationProperty();
                gtBrand.UpdateTo(brand);
                return gtBrand;
            }
        }




        public void Delete(int modelId, int propertyId)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var brand = db.ModelModificationProperties.SingleOrDefault(x => x.ModelId == modelId && x.PropertyId == propertyId);

                if (brand != null)
                {
                    db.ModelModificationProperties.DeleteOnSubmit(brand);
                    db.SubmitChanges();
                }
            }
        }

        public void DeleteByModelAndProp(int modelId, int propId)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var brand = db.ModelModificationProperties.ToList().Where(x => x.ModelId == modelId && x.PropertyId == propId);

                foreach (var modelModificationProperty in brand)
                {
                    db.ModelModificationProperties.DeleteOnSubmit(modelModificationProperty);
                    db.SubmitChanges();
                }
            }
        }

        public void DeleteByProperty(int proprertyId)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var brand = db.ModelModificationProperties.ToList().Where(x => x.PropertyId == proprertyId);

                foreach (var modelModificationProperty in brand)
                {
                    db.ModelModificationProperties.DeleteOnSubmit(modelModificationProperty);
                    db.SubmitChanges();
                }
            }
        }

        public List<GTModelModificationProperty> GetByModel(int modelId)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var props =
                 from p in db.ModelModificationProperties
                 where p.ModelId == modelId
                 select p;

                var brands = props.ToList();

                var ret = new List<GTModelModificationProperty>();

                foreach (var brand in brands)
                {
                    var gtBrand = new GTModelModificationProperty();
                    gtBrand.UpdateTo(brand);
                    ret.Add(gtBrand);
                }
                return ret;
            }
        }


        public List<SimpleObject> SimpleGetByModel(int modelId)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var props =
                 from p in db.ModelModificationProperties
                 where p.ModelId == modelId
                 select p;

                var brands = props.ToList();

                var ret = new List<SimpleObject>();

                foreach (var brand in brands)
                {
                    var gtBrand = new SimpleObject { Prop1 = brand.ModelId.ToString(), Prop2 = brand.PropertyId.ToString() };
                    ret.Add(gtBrand);

                }
                return ret;
            }
        }


    }
}
